package com.itmk.web.sys_role.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.Result;
import com.itmk.web.sys_menu.service.SysMenuService;
import com.itmk.web.sys_role.entity.RoleParm;
import com.itmk.web.sys_role.entity.SelectItem;
import com.itmk.web.sys_role.entity.SysRole;
import com.itmk.web.sys_role.service.SysRoleService;
import com.itmk.web.sys_role_menu.entity.SaveMenuParm;
import com.itmk.web.sys_role_menu.service.RoleMenuSrevice;
import org.apache.commons.lang.StringUtils;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private RoleMenuSrevice roleMenuSrevice;

    //新增
    @PostMapping
    public Result<Object> add(@RequestBody SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        if (sysRoleService.save(sysRole)) {
            return Result.ok().message("新增角色成功");
        }
        return Result.fail().message("新增角色失败");
    }

    //编辑
    @PutMapping
    public Result<Object> update(@RequestBody SysRole sysRole) {
        sysRole.setUpdateTime(new Date());
        if (sysRoleService.updateById(sysRole)) {
            return Result.ok().message("编辑角色成功");
        }
        return Result.fail().message("编辑角色失败");
    }

    //删除
    @DeleteMapping("/{roleId}")
    public Result<Object> delete(@PathVariable(value="roleId") Long roleId) {

        System.out.println("roleId:" + roleId);
        if (sysRoleService.removeById(roleId)) {
            return Result.ok().message("删除角色成功");
        }
        return Result.fail().message("删除角色失败");
    }

    //查询列表
    @GetMapping("/getList")
    public Result getList(RoleParm parm) {
        //构造分页对象
        IPage<SysRole> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        //构造查询条件
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        //如果有传角色名称，就构造进条件里
        if (StringUtils.isNotEmpty(parm.getRoleName())) {
            queryWrapper.lambda().like(SysRole::getRoleName, parm.getRoleName());
        }
        //默认按照创建时间升序排列
        queryWrapper.lambda().orderByAsc(SysRole::getCreateTime);
        IPage<SysRole> list = sysRoleService.page(page, queryWrapper);
        return Result.ok(list).message("分页查询角色列表成功");
    }

    //角色下拉数据列表
    @GetMapping("/selectList")
    public Result getSelectList() {
        List<SysRole> list = sysRoleService.list();
        //返回数据
        List<SelectItem> selectItems = new ArrayList<>();
        for (SysRole role : list) {
            SelectItem selectItem = new SelectItem();
            selectItem.setLabel(role.getRoleName());
            selectItem.setValue(role.getRoleId());
            selectItems.add(selectItem);
        }
        
        return Result.ok(selectItems).message("角色下拉数据列表成功");
    }
    //保存当前角色拥有的菜单
    @PostMapping("saveRoleMenu")
    public Result<Object> saveRoleMenu(@RequestBody SaveMenuParm parm) {
        roleMenuSrevice.saveRoleMenu(parm);
        return Result.ok().message("为角色分配菜单成功");
    }
}